package hu.cskassai.sad.gui.panels;

import hu.cskassai.sad.business.GraphManager;
import hu.cskassai.sad.model.graph.Edge;
import hu.cskassai.sad.model.graph.Vertex2D;

import java.awt.BorderLayout;
import java.awt.Font;
import java.util.Collection;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

public class AlgorithmDetailPanel extends JPanel {
	
	private static final String[] COLUMS = new String[] {
		"Value", "Name"
	};
	
	private final GraphManager<Vertex2D, Edge> graphManager;
	
	private final JLabel time;
	
	private final JLabel actMemory;
	
	private final JLabel maxMemory;
	
	private final JLabel stepNumber;
	private final JTable table_2;
	
	/**
	 * Create the panel.
	 */
	public AlgorithmDetailPanel(final GraphManager<Vertex2D, Edge> graphManager) {
		setLayout(null);
		this.graphManager = graphManager;
		final JLabel lblNewLabel = new JLabel("Number expanded vertices:");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(0, 102, 250, 15);
		add(lblNewLabel);
		
		final JLabel lblNewLabel_1 = new JLabel("Actual memory usage");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_1.setBounds(0, 141, 246, 78);
		add(lblNewLabel_1);
		
		final JLabel lblNewLabel_2 = new JLabel("Max memory usage");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_2.setBounds(0, 231, 246, 15);
		add(lblNewLabel_2);
		
		time = new JLabel("0");
		time.setHorizontalAlignment(SwingConstants.CENTER);
		time.setFont(new Font("Dialog", Font.BOLD, 23));
		time.setBounds(0, 114, 246, 60);
		add(time);
		
		actMemory = new JLabel("0");
		actMemory.setHorizontalAlignment(SwingConstants.CENTER);
		actMemory.setFont(new Font("Dialog", Font.BOLD, 23));
		actMemory.setBounds(0, 186, 246, 60);
		add(actMemory);
		
		maxMemory = new JLabel("0");
		maxMemory.setHorizontalAlignment(SwingConstants.CENTER);
		maxMemory.setFont(new Font("Dialog", Font.BOLD, 23));
		maxMemory.setBounds(0, 240, 246, 60);
		add(maxMemory);
		
		final JPanel panel = new JPanel();
		panel.setBounds(0, 288, 250, 255);
		add(panel);
		panel.setLayout(new BorderLayout(0, 0));
		
		final JScrollPane scrollPane = new JScrollPane();
		panel.add(scrollPane, BorderLayout.CENTER);
		
		table_2 = new JTable();
		final DefaultTableModel defaultTableModel = new DefaultTableModel(
			new Object[][] {
				{ null, null },
				{ null, null },
			},
			COLUMS
			);
		
		table_2.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Value", "Name"
			}
			));
		scrollPane.setColumnHeaderView(table_2);
		
		final JLabel lblNumberOfSteps = new JLabel("Number of steps");
		lblNumberOfSteps.setBounds(56, 12, 136, 15);
		add(lblNumberOfSteps);
		
		stepNumber = new JLabel("0");
		stepNumber.setHorizontalAlignment(SwingConstants.CENTER);
		stepNumber.setFont(new Font("Dialog", Font.BOLD, 23));
		stepNumber.setBounds(0, 39, 246, 51);
		add(stepNumber);
		
	}
	
	public void refresh() {
		time.setText(String.valueOf(graphManager.getTime()));
		maxMemory.setText(String.valueOf(graphManager.getMaxMemory()));
		actMemory.setText(String.valueOf(graphManager.getActualMemory()));
		stepNumber.setText(String.valueOf(graphManager.getStepNumber()));
		this.repaint();
		
	}
	
	public void setVertexProperties(final Collection<Object[]> properties) {
		final DefaultTableModel defaultTableModel = new DefaultTableModel(COLUMS, 0);
		for (final Object[] objects : properties) {
			defaultTableModel.addRow(objects);
		}
		table_2.setModel(defaultTableModel);
		this.repaint();
	}
}
